//
//  YPViewController.m
//  WeiDuYY
//
//  Created by eluosi on 2019/1/24.
//  Copyright © 2019 W ke杨. All rights reserved.
//

#import "YPViewController.h"
#import "OneViewController.h"
#import "TwoViewController.h"
#import "ThreeViewController.h"
#import <SDAutoLayout.h>
@interface YPViewController ()<UIScrollViewDelegate>{
    UIScrollView *scr;
    UIButton *bun;
    UIScrollView *center;
}
@property (nonatomic,strong)UIButton *selbun;
@end

@implementation YPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
//    self.view.backgroundColor = [UIColor redColor];

//    self.navigationItem.title=@"网易新闻";
    //添加子控制器
    [self setUpVc];
    //添加顶部滚动式图
    [self setTopVc];
    //添加点击按钮
    [self setUpBun];
    //设置中间滚动
    [self setCenterVc];
}
- (void)setUpVc{
    OneViewController *one=[[OneViewController alloc]init];
    one.title=@"热门电影";
    [self addChildViewController:one];
    
    TwoViewController *two=[[TwoViewController alloc]init];
    two.title=@"正在热映";
    [self addChildViewController:two];
    
    ThreeViewController *three=[[ThreeViewController alloc]init];
    three.title=@"即将上映";
    [self addChildViewController:three];
    
}
- (void)setTopVc{
//    scr=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    scr = [UIScrollView new];
    
    scr.backgroundColor=[UIColor whiteColor];
    scr.bounces=NO  ;
//    scr.backgroundColor = [UIColor redColor];
    [self.view addSubview:scr];
    //顶部视图
    scr.sd_layout.topSpaceToView(self.view, 0).leftSpaceToView(self.view, 0).rightSpaceToView(self.view, 0).widthIs(self.view.frame.size.width).heightIs(180);
}
- (void)setUpBun{
    NSInteger count=self.childViewControllers.count;
    for (NSInteger i=0; i<count; i++) {
        bun=[[UIButton alloc]initWithFrame:CGRectMake(i*157, 30, 100, 20)];
//        bun = [[UIButton alloc] init];
//        bun.sd_layout.topSpaceToView(self.view, 0).leftSpaceToView(self.view, 150).rightSpaceToView(self.view, 50).widthIs(100).heightIs(30);
      
//        bun.backgroundColor = [UIColor greenColor];
       
        //设置边框颜色
        bun.layer.borderColor = [[UIColor blackColor] CGColor];
        //设置边框宽度
        bun.layer.borderWidth = 1.0f;
//        //给按钮设置角的弧度
        bun.layer.cornerRadius = 8.0f;
        //设置背景颜色
//        bun.backgroundColor = [UIColor redColor];
        bun.layer.masksToBounds = YES;
        
        bun.tag=i;
        NSLog(@"tag :%ld",bun.tag);
        if (i==0) {
            [self ppp:bun];
            
        }
        UIViewController *vc=self.childViewControllers[i];
        [bun setTitle:vc.title forState:UIControlStateNormal];
        [bun setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [bun setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [bun addTarget:self action:@selector(ppp:) forControlEvents:UIControlEventTouchUpInside];
        [scr addSubview:bun];
        
       
        
         scr.contentSize=CGSizeMake(count *100, 0);
//          bun.sd_layout.topSpaceToView(self.view, i*157).leftSpaceToView(self.view, 50).rightSpaceToView(self.view, 50).widthIs(100).heightIs(20);
    }
   
}
- (void)setCenterVc{
//中心视图
    center = [[UIScrollView alloc] init];
    
    center.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:center];
    center.pagingEnabled=YES;
    center.bounces=NO;
    center.delegate=self;
    center.contentSize=CGSizeMake([UIScreen mainScreen].bounds.size.width*self.childViewControllers.count, 0);
    for (int i=0; i<self.childViewControllers.count; i++) {
        UIViewController *vc=self.childViewControllers[i];
        vc.view.frame=CGRectMake(self.view.frame.size.width*i, 0, self.view.frame.size.width, self.view.frame.size.height);
        [center addSubview:vc.view];
        //中心位置
        center.sd_layout.topSpaceToView(self.view, 108).leftSpaceToView(self.view, 0).rightSpaceToView(self.view, 0).widthIs(self.view.frame.size.width).heightIs(self.view.frame.size.height-108);
    }
}
- (void)ppp:(UIButton *)sender{
    
    [self selbun:sender];
    //滚动到对应页面的偏移量
    CGFloat ofsetX = sender.tag*self.view.frame.size.width;
    NSLog(@"%ld",sender.tag);
    //设置中间滚动式图的偏移量
    center.contentOffset=CGPointMake(ofsetX, 0);
    //添加对应子控制器view到对应的位置
    UIViewController *vc=self.childViewControllers[sender.tag];
    //设置子控制器view的位置
    vc.view.frame=CGRectMake(ofsetX, 0, self.view.frame.size.width, self.view.frame.size.height);
    [center addSubview:vc.view];
}
- (void)selbun:(UIButton *)sender{
    _selbun.selected=NO;
    sender.selected=YES;
    _selbun=sender;
}

@end
